package com.songyang.blog.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.songyang.blog.common.lang.Result;
import com.songyang.blog.entity.Post;
import com.songyang.blog.vo.PostVO;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {
    @PostMapping("/jie-set")
    @ResponseBody
    public Result jetSet(Long id, Integer rank, String field) {

        Post post = postService.getById(id);
        Assert.notNull(post, "该帖子已被删除");
        if ("delete".equals(field)) {//删除操作
            postService.removeById(id);
            return Result.success();
        } else if ("status".equals(field)) {
            post.setRecommend(rank == 1);//如果等于1就加精，不是的话就取消加精
        } else if ("stick".equals(field)) {
            post.setLevel(rank); //rank 0代表取消，1代表操作
        }
        postService.updateById(post);
        return Result.success();
    }

    @ResponseBody
    @PostMapping("/initEsData")
    public Result initEsData() {
        int size = 10000;
        long total=0;
        Page page = new Page();
        page.setSize(size);
        for (int i = 1; i < 10000000; i++) {
            page.setCurrent(i);//第几页
            IPage<PostVO> iPage = postService.paging(page, null, null, null, null);
            int num = searchService.initEsData(iPage.getRecords());
            total+=num;//计算一共多少条记录
            //当一页的内容小于我们要展示的数量，就说明没有数据需要搜索了，就可以跳出循环了
            if (page.getRecords().size() < size) {
                break;
            }
        }
           return Result.success("Es索引初始化成功，共同步"+total+"条记录",null);
    }
}









